package com.huilai.platform.country_platform.controller;

import com.huilai.platform.country_platform.core.Msg;
import com.huilai.platform.country_platform.core.Result;
import com.huilai.platform.country_platform.dto.*;
import com.huilai.platform.country_platform.model.*;
import com.huilai.platform.country_platform.repository.InformationRecruitmentReposity;
import com.huilai.platform.country_platform.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：洪霓
 * 日期：2023/2/19
 * 注释：
 */
@Controller
@RequestMapping("/system")
public class SystemController {

    @Value("${file.upload.path}")
    private String filePath;

    @Value("${file.upload.relative}")
    private String fileRelativePath;

    @Autowired
    private SystemService systemService;

    @Autowired
    private UserService userService;

    @Autowired
    private PartyService partyService;

    @Autowired
    private VillageService villageService;

    @Autowired
    private InformationService informationService;

    @Autowired
    private AttractionsService attractionsService;

    @Autowired
    private ChatService chatService;

    @RequestMapping("/loginUI")
    public String loginUI(){
        return "system/login/login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public Result login(SysUser sysUser, HttpServletResponse response, HttpSession session) {
        return systemService.login(sysUser,response,session);
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        System.out.println("dddddddd");
        systemService.logout(request, response, session);
        //SpringMVC重定向
        return "system/login/login";
//        return "redirect:/system/loginUI";
    }

    @RequestMapping("/myInformation")
    public String myInformation(Model model, HttpSession httpSession){
        User user = (User) httpSession.getAttribute("user");
        Integer fromId = userService.getUserByUsername(user.getUsername()).getId();
        List<ChatListDTO> chatListDTOList = chatService.getFromUserChatList(fromId);
        model.addAttribute("chatListDTOList",chatListDTOList);
        model.addAttribute("fromName", user.getUsername());
        model.addAttribute("activeUrl1", "indexActive");
        model.addAttribute("activeUrl2", "indexActive");
        model.addAttribute("pageTopBarInfo","管理员中心");
        return "system/myInformation";
    }
    @GetMapping("/intoMyInformation")
    public String intoMyInformation(@RequestParam("linkId")Integer linkId, @RequestParam("toName")String toName,
                                    @RequestParam(value = "pageNum", defaultValue = "0") Integer pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                    Model model,
                                    HttpSession httpSession){
        User user = (User) httpSession.getAttribute("user");
        Integer fromId = userService.getUserByUsername(user.getUsername()).getId();
        System.out.println(fromId);
        List<ChatMessageDTO> chatMessageDTOList = chatService.getRecentChatRecords(linkId, fromId, userService.getUserByUsername(toName).getId(),pageNum, pageSize);
        List<ChatListDTO> chatListDTOList = chatService.getFromUserChatList(fromId);
        model.addAttribute("linkId", linkId);
        model.addAttribute("toName", toName);
        model.addAttribute("toId",userService.getUserByUsername(toName).getId());
        model.addAttribute("fromId", fromId);
        model.addAttribute("fromName", user.getUsername());
        model.addAttribute("chatListDTOList", chatListDTOList);
        model.addAttribute("chatMessageDTOList", chatMessageDTOList);
        model.addAttribute("activeUrl1", "indexActive");
        model.addAttribute("activeUrl2", "indexActive");
        model.addAttribute("pageTopBarInfo","管理员中心");

        //获取最近的聊天记录
        return "system/intoMyInformation";

    }

//    @RequestMapping("/user/toUserinfoListUI")
//    public String toUserinfoListUI(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
//                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
//                                   Model model){
//        //用户信息管理-用户信息
//        PageHelper.startPage(pageNum, pageSize);
//        List<User> userList = systemService.toUserinfoListUI();
//        for (User user: userList){
//            if (null == user.getUsername()||user.getUsername().isEmpty()){
//                user.setUsername("未填写");
//            }
//            if (null == user.getNickname()||user.getNickname().isEmpty()){
//                user.setNickname("未填写");
//            }
//            if (null == user.getSex()||user.getSex().isEmpty()){
//                user.setSex("未填写");
//            }
//            if (null == user.getTelephone()||user.getTelephone().isEmpty()){
//                user.setTelephone("未填写");
//            }
//            if (null == user.getEducation()||user.getEducation().isEmpty()){
//                user.setEducation("未填写");
//            }
//            if (null == user.getPosition()||user.getPosition().isEmpty()){
//                user.setPosition("未填写");
//            }
//            if (null == user.getPolitical()||user.getPolitical().isEmpty()){
//                user.setPolitical("未填写");
//            }
//            if (null == user.getTown()||user.getTown().isEmpty()){
//                user.setTown("未填写");
//            }
//            if (null == user.getVillage()||user.getVillage().isEmpty()){
//                user.setVillage("未填写");
//            }
//        }
//
//        // 使用PageInfo包装查询后的结果，并交给页面处理
//        // PageInfo封装了详细的分页信息，包括我们查询出来的数据，还可以传入连续显示的页数（5）
//        PageInfo<User> userPageInfo = new PageInfo<>(userList, 5);
//        System.out.println(userPageInfo.getPageSize());
//        System.out.println(userPageInfo.getPageNum());
//        System.out.println(userPageInfo.getPages());
//        model.addAttribute("userList", userList);
//        model.addAttribute("userPageInfo", userPageInfo);
//        model.addAttribute("activeUrl1", "userInfoActive");
//        model.addAttribute("activeUrl2", "userInfoActive");
//        model.addAttribute("pageTopBarInfo","用户信息");
//        return "system/user/userinfo";
//    }


    @RequestMapping("/user/toUserinfoListUI")
    public String toUserinfoListUI(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                   Model model){
        //用户信息管理-用户信息
        if (pageNum == null){
            pageNum = 1;
        }
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<User> page = systemService.toUserinfoListUI(pageable);
        for (User user: page.getContent()){
            if (null == user.getUsername()||user.getUsername().isEmpty()){
                user.setUsername("未填写");
            }
            if (null == user.getNickname()||user.getNickname().isEmpty()){
                user.setNickname("未填写");
            }
            if (null == user.getSex()||user.getSex().isEmpty()){
                user.setSex("未填写");
            }
            if (null == user.getTelephone()||user.getTelephone().isEmpty()){
                user.setTelephone("未填写");
            }
            if (null == user.getEducation()||user.getEducation().isEmpty()){
                user.setEducation("未填写");
            }
            if (null == user.getPosition()||user.getPosition().isEmpty()){
                user.setPosition("未填写");
            }
            if (null == user.getPolitical()||user.getPolitical().isEmpty()){
                user.setPolitical("未填写");
            }
            if (null == user.getTown()||user.getTown().isEmpty()){
                user.setTown("未填写");
            }
            if (null == user.getVillage()||user.getVillage().isEmpty()){
                user.setVillage("未填写");
            }
        }

        List<String> educationList = new ArrayList<>();
        educationList.add("无");
        educationList.add("小学");
        educationList.add("初中");
        educationList.add("高中");
        educationList.add("本科");
        educationList.add("硕士");
        educationList.add("博士及以上");

        List<String> politicalList = new ArrayList<>();
        politicalList.add("中国共产党党员");
        politicalList.add("中共预备党员");
        politicalList.add("共青团员");
        politicalList.add("民主党派人士");
        politicalList.add("无党派民主人士");
        politicalList.add("群众");

        List<String> villageList = new ArrayList<>();
        villageList.add("仙庵村");
        villageList.add("锡溪村");
        villageList.add("宁寨村");
        villageList.add("顶溪村");
        villageList.add("东埔村");
        villageList.add("四美村");
        villageList.add("京陇村");
        villageList.add("点铺村");
        villageList.add("田墘村");
        villageList.add("里行村");
        villageList.add("华园村");

        model.addAttribute("page", page);
        model.addAttribute("educationList",educationList);
        model.addAttribute("politicalList",politicalList);
        model.addAttribute("villageList",villageList);
        model.addAttribute("activeUrl1", "userInfoActive");
        model.addAttribute("activeUrl2", "userInfoActive");
        model.addAttribute("pageTopBarInfo","用户信息管理");
        return "system/user/userinfo";
    }

    @GetMapping("/user/getUserById")
    @ResponseBody
    public Msg getUserById(@RequestParam("id")Integer id){
        // 根据id获得user实体, 用于更新用户信息时回显用户信息（管理员）
        User user = userService.getUserById(id);
        return Msg.success().add("user",user);
    }

    @PostMapping("/user/addUser")
    @ResponseBody
    public Msg addUser(User user){
        Integer result = userService.addUser(user);
        System.out.println(result);
        if (result == 1){
            return Msg.success();
        }
        return Msg.fail();

    }

    @PutMapping("/user/updateUser")
    @ResponseBody
    public Msg updateUser(@RequestParam("userId") Integer id, User user) {
//        更新user实体
        user.setId(id);
        Integer result = userService.updateUser(user);
        if (result == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @DeleteMapping("/user/deleteUserById")
    @ResponseBody
    public Msg deleteUserById(@RequestParam("id")Integer id){
        Integer result = userService.deleteUserById(id);
        if (result == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @RequestMapping("/party/toPartyCommitteeListUI")
    public String toPartyCommitteeListUI(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                         Model model, @ModelAttribute("searchName") String searchName){
//        智慧党建-党务管理
        if (pageNum == null){
            pageNum = 1;
        }
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<VillageCommittee> page = systemService.toPartyCommitteeListUI(pageable, searchName);
        List<String>unitList = new ArrayList<>();
        unitList.add("党政综合办公室");
        unitList.add("党建工作办公室");
        unitList.add("人大办公室");
        unitList.add("纪检监察办公室");
        model.addAttribute("page", page);
        model.addAttribute("unitList", unitList);
        model.addAttribute("activeUrl1", "partyActive");
        model.addAttribute("activeUrl2", "partyCommitteeActive");
        model.addAttribute("pageTopBarInfo","智慧党建管理");
        return "system/party/committee";
    }

    @PostMapping("/party/addPartyCommittee")
    @ResponseBody
    public Msg addPartyCommittee(VillageCommittee villageCommittee){
        Integer result = villageService.addVillageCommittee(villageCommittee);
        System.out.println(result);
        if (result == 1){
            return Msg.success();
        }
        return Msg.fail();
    }

    @GetMapping("/party/getCommitteeById")
    @ResponseBody
    public Msg getCommitteeById(@RequestParam("id")Integer id){
        // 根据id获得PartyActivities实体
        VillageCommittee villageCommittee = villageService.getCommitteeById(id);
        return Msg.success().add("villageCommittee",villageCommittee);
    }

    @PutMapping("/party/updateCommittee")
    @ResponseBody
    public Msg updateCommittee(@RequestParam("id") Integer id, VillageCommittee villageCommittee) {
        villageCommittee.setId(id);
        Integer result = villageService.updateCommittee(villageCommittee);
        if (result == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @DeleteMapping("/party/deleteCommitteeById")
    @ResponseBody
    public Msg deleteCommitteeById(@RequestParam("id")Integer id){
        Integer result = villageService.deleteCommitteeById(id);
        if (result == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @RequestMapping("/party/toPartyActivitiesListUI")
    public String toPartyActivitiesListUI(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                   Model model, @ModelAttribute("searchName") String searchName){
//        智慧党建-党员活动等单元
        if (pageNum == null){
            pageNum = 1;
        }
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<PartyActivities> page = systemService.toPartyActivitiesListUI(pageable, searchName);
        List<String> typeList = new ArrayList<>();
        typeList.add("党员活动");
        typeList.add("党建动态");
        typeList.add("三会一课");
        model.addAttribute("page", page);
        model.addAttribute("typeList", typeList);
        model.addAttribute("activeUrl1", "partyActive");
        model.addAttribute("activeUrl2", "partyActivitiesActive");
        model.addAttribute("pageTopBarInfo","智慧党建管理");
        return "system/party/activities";

    }

    @PostMapping("/party/addPartyActivities")
    @ResponseBody
    public Msg addPartyActivities(PartyActivities partyActivities){
        Integer result = partyService.addPartyActivities(partyActivities);
        System.out.println(result);
        if (result == 1){
            return Msg.success();
        }
        return Msg.fail();
    }

    @GetMapping("/party/getActivitiesById")
    @ResponseBody
    public Msg getActivitiesById(@RequestParam("id")Integer id){
        // 根据id获得PartyActivities实体
        PartyActivities partyActivities = partyService.getActivitiesById(id);
        return Msg.success().add("partyActivities",partyActivities);
    }

    @PutMapping("/party/updateActivities")
    @ResponseBody
    public Msg updateActivities(@RequestParam("id") Integer id, PartyActivities partyActivities) {
        partyActivities.setId(id);
        Integer result = partyService.updateActivities(partyActivities);
        if (result == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @DeleteMapping("/party/deleteActivitiesById")
    @ResponseBody
    public Msg deleteActivitiesById(@RequestParam("id")Integer id){
        Integer result = partyService.deleteActivitiesById(id);
        if (result == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }
    @RequestMapping("/village/toVillageCommitteeListUI")
    public String toVillageCommitteeListUI(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                         Model model, @ModelAttribute("searchName") String searchName){
//        村务公开-村委管理
        if (pageNum == null){
            pageNum = 1;
        }
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<VillageCommittee> page = systemService.toVillageCommitteeListUI(pageable, searchName);
        List<String>unitList = new ArrayList<>();
        unitList.add("公共服务办公室");
        unitList.add("综合治理办公室");
        unitList.add("综合行政执法办公室");
        unitList.add("经济发展办公室");
        unitList.add("应急管理办公室");
        unitList.add("农业农村办公室");
        model.addAttribute("page", page);
        model.addAttribute("unitList", unitList);
        model.addAttribute("activeUrl1", "villageActive");
        model.addAttribute("activeUrl2", "villageCommitteeActive");
        model.addAttribute("pageTopBarInfo","乡务公开管理");
        return "system/village/committee";
    }

    @PostMapping("/village/addVillageCommittee")
    @ResponseBody
    public Msg addVillageCommittee(VillageCommittee villageCommittee){
        Integer result = villageService.addVillageCommittee(villageCommittee);
        System.out.println(result);
        if (result == 1){
            return Msg.success();
        }
        return Msg.fail();
    }

    @RequestMapping("/village/toVillageActivitiesListUI")
    public String toVillageActivitiesListUI(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                          Model model, @ModelAttribute("searchName") String searchName){
//        村务公开-政务公开等单元
        if (pageNum == null){
            pageNum = 1;
        }
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<VillageActivities> page = systemService.toVillageActivitiesListUI(pageable, searchName);
        List<String> typeList = new ArrayList<>();
        typeList.add("乡风文明");
        typeList.add("农技知识");
        typeList.add("办事指南");
        typeList.add("惠农政策");
        typeList.add("政务公开");
        typeList.add("政务宣传");
        typeList.add("政策法规");
        typeList.add("普法宣传");
        typeList.add("财务公开");
        model.addAttribute("page", page);
        model.addAttribute("typeList", typeList);
        model.addAttribute("activeUrl1", "villageActive");
        model.addAttribute("activeUrl2", "villageActivitiesActive");
        model.addAttribute("pageTopBarInfo","乡务公开管理");
        return "system/village/activities";

    }

    @PostMapping("/village/addVillageActivities")
    @ResponseBody
    public Msg addVillageActivities(VillageActivities villageActivities){
        Integer result = villageService.addVillageActivities(villageActivities);
        System.out.println(result);
        if (result == 1){
            return Msg.success();
        }
        return Msg.fail();
    }

    @GetMapping("/village/getVillageActivitiesById")
    @ResponseBody
    public Msg getVillageActivitiesById(@RequestParam("id")Integer id){
        // 根据id获得VillageActivities实体
        VillageActivities villageActivities = villageService.getVillageActivitiesById(id);
        return Msg.success().add("villageActivities",villageActivities);
    }

    @PutMapping("/village/updateVillageActivities")
    @ResponseBody
    public Msg updateVillageActivities(@RequestParam("id") Integer id, VillageActivities villageActivities) {
        villageActivities.setId(id);
        Integer result = villageService.updateVillageActivities(villageActivities);
        if (result == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @DeleteMapping("/village/deleteVillageActivitiesById")
    @ResponseBody
    public Msg deleteVillageActivitiesById(@RequestParam("id")Integer id){
        Integer result = villageService.deleteVillageActivitiesById(id);
        if (result == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @RequestMapping("/village/toVillageFeedbackListUI")
    public String toVillageFeedbackListUI(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                            Model model, @ModelAttribute("searchName") String searchName){
        //乡务公开-民事反馈等单元
        if (pageNum == null){
            pageNum = 1;
        }
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<FeedbackDTO> page = systemService.toVillageFeedbackListUI(pageable, searchName);
        List<String> typeList = new ArrayList<>();
        typeList.add("检举控告");
        typeList.add("群众诉求");
        typeList.add("批评建议");
        model.addAttribute("page", page);
        model.addAttribute("typeList", typeList);
        model.addAttribute("activeUrl1", "villageActive");
        model.addAttribute("activeUrl2", "villageFeedbackActive");
        model.addAttribute("pageTopBarInfo","乡务公开管理");
        return "system/village/feedback";
    }

    @GetMapping("/village/getVillageFeedbackById")
    @ResponseBody
    public Msg getVillageFeedbackById(@RequestParam("id")Integer id){
        // 根据id获得VillageFeedback实体
        FeedbackDTO feedbackDTO = villageService.getVillageFeedbackById(id);
        return Msg.success().add("feedbackDTO",feedbackDTO);
    }

    @PutMapping("/village/changeDealAudit")
    @ResponseBody
    public Msg changeDealAudit(UserFeedback userFeedback){
        UserFeedback userFeedback1 = villageService.findUserFeedbackByFeedbackId(userFeedback.getFeedbackId());
        Integer result = villageService.changeFeedbackAuditStatusById(userFeedback.getFeedbackId(), 1);
//        userRecruitment1.setDescribe(userRecruitment.getDescribe());
        Integer result1 = villageService.updateUserFeedback(userFeedback1.getId(), userFeedback.getDealDescribe());
        if (result == 1 && result1 == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @RequestMapping("/village/changeEndAudit")
    @ResponseBody
    public Msg changeEndAudit(UserFeedback userFeedback){
        Integer result = villageService.changeFeedbackAuditStatusById(userFeedback.getFeedbackId(), 2);
        UserFeedback userFeedback1 = villageService.findUserFeedbackByFeedbackId(userFeedback.getFeedbackId());
        Integer result1 = villageService.updateUserFeedback2(userFeedback1.getId(), userFeedback.getEndDescribe());
        if (result == 1 && result1 == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @RequestMapping("/information/toRecruitmentListUI")
    public String toRecruitmentListUI(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                      Model model, @ModelAttribute("searchName") String searchName){
        //便民信息管理-招工就业
        if (pageNum == null){
            pageNum = 1;
        }
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<RecruitmentHasUserDTO> page = systemService.toRecruitmentListUI(pageable, searchName);
        model.addAttribute("page", page);
        model.addAttribute("activeUrl1","informationActive");
        model.addAttribute("activeUrl2", "recruitmentActive");
        model.addAttribute("pageTopBarInfo","便民信息管理");
        return "system/information/recruitment";
    }

    @PostMapping("/information/addRecruitment")
    @ResponseBody
    public Msg addRecruitment(InformationRecruitment informationRecruitment, HttpSession session){
        Integer result = informationService.addAdminRecruitment(informationRecruitment, session);
        System.out.println(result);
        if (result == 1){
            return Msg.success();
        }
        return Msg.fail();
    }

    @GetMapping("/information/getRecruitmentHasUserDTOById")
    @ResponseBody
    public Msg getRecruitmentHasUserDTOById(@RequestParam("id")Integer id){
        // 根据id获得InformationRecruitment实体
         RecruitmentHasUserDTO recruitmentHasUserDTO = informationService.getRecruitmentHasUserDTOById(id);
        return Msg.success().add("recruitmentHasUserDTO",recruitmentHasUserDTO);
    }

    @GetMapping("/information/getRecruitmentById")
    @ResponseBody
    public Msg getRecruitmentById(@RequestParam("id")Integer id){
        // 根据id获得InformationRecruitment实体
        InformationRecruitment informationRecruitment = informationService.findRecruitmentById(id);
        return Msg.success().add("informationRecruitment",informationRecruitment);
    }

    @PutMapping("/information/updateRecruitment")
    @ResponseBody
    public Msg updateRecruitment(@RequestParam("id") Integer id, InformationRecruitment informationRecruitment) {
        informationRecruitment.setId(id);
        Integer result = informationService.updateRecruitmentAll(informationRecruitment);
        if (result == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @DeleteMapping("/information/deleteRecruitmentById")
    @ResponseBody
    public Msg deleteRecruitmentById(@RequestParam("id")Integer id){
        Integer result = informationService.deleteRecruitmentById(id);
        if (result == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @PutMapping("/information/changeRecruitmentAuditStatusById")
    @ResponseBody
    public Msg changeRecruitmentAuditStatusById(@RequestParam("id")Integer id,@RequestParam("auditStatus")Integer auditStatus){
        Integer result = informationService.changeRecruitmentAuditStatusById(id, auditStatus);
        if (result == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @PutMapping("/information/changeAuditNoSuccess")
    @ResponseBody
    public Msg changeAuditNoSuccess(UserRecruitment userRecruitment){
        Integer result = informationService.changeRecruitmentAuditStatusById(userRecruitment.getRecruitmentId(), 2);
        UserRecruitment userRecruitment1 = informationService.findUserRecruitmentByRecruitmentId(userRecruitment.getRecruitmentId());
//        userRecruitment1.setDescribe(userRecruitment.getDescribe());
        Integer result1 = informationService.updateUserRecruitment(userRecruitment1.getId(), userRecruitment.getDescribe());
        if (result == 1 && result1 == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @RequestMapping("/attractions/toBuildingListUI")
    public String toBuildingListUI(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                   Model model, @ModelAttribute("searchName") String searchName){
        //仙庵城事管理-乡镇建筑
        if (pageNum == null){
            pageNum = 1;
        }
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<AttractionsBuilding> page = systemService.toBuildingListUI(pageable, searchName);
        model.addAttribute("page",page);
        model.addAttribute("activeUrl1","attractionsActive");
        model.addAttribute("activeUrl2", "buildingActive");
        model.addAttribute("pageTopBarInfo","仙庵城事管理");
        return "system/attractions/building";
    }

    @PostMapping("/attractions/addBuilding")
    @ResponseBody
    public Msg addBuilding(AttractionsBuilding attractionsBuilding){
        Integer result = attractionsService.addBuilding(attractionsBuilding);
        System.out.println(result);
        if (result == 1){
            return Msg.success();
        }
        return Msg.fail();
    }

    @GetMapping("/attractions/getBuildingById")
    @ResponseBody
    public Msg getBuildingById(@RequestParam("id")Integer id){
        // 根据id获得PartyActivities实体
        AttractionsBuilding attractionsBuilding = attractionsService.getBuildingById(id);
        return Msg.success().add("attractionsBuilding",attractionsBuilding);
    }

    @PutMapping("/attractions/updateBuilding")
    @ResponseBody
    public Msg updateBuilding(@RequestParam("id") Integer id, AttractionsBuilding attractionsBuilding) {
        attractionsBuilding.setId(id);
        Integer result = attractionsService.updateBuilding(attractionsBuilding);
        if (result == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @DeleteMapping("/attractions/deleteBuildingById")
    @ResponseBody
    public Msg deleteBuildingById(@RequestParam("id")Integer id){
        Integer result = attractionsService.deleteBuildingById(id);
        if (result == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @RequestMapping("/attractions/toWaterscapeListUI")
    public String toWaterscapeListUI(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                   Model model, @ModelAttribute("searchName") String searchName){
        //仙庵城事管理-乡镇建筑
        if (pageNum == null){
            pageNum = 1;
        }
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<AttractionsWaterscape> page = systemService.toWaterscapeListUI(pageable, searchName);
        model.addAttribute("page",page);
        model.addAttribute("activeUrl1","attractionsActive");
        model.addAttribute("activeUrl2", "waterscapeActive");
        model.addAttribute("pageTopBarInfo","仙庵城事管理");
        return "system/attractions/waterscape";
    }

    @PostMapping("/attractions/addWaterscape")
    @ResponseBody
    public Msg addWaterscape(AttractionsWaterscape attractionsWaterscape){
        Integer result = attractionsService.addWaterscape(attractionsWaterscape);
        System.out.println(result);
        if (result == 1){
            return Msg.success();
        }
        return Msg.fail();
    }

    @GetMapping("/attractions/getWaterscapeById")
    @ResponseBody
    public Msg getWaterscapeById(@RequestParam("id")Integer id){
        // 根据id获得PartyActivities实体
        AttractionsWaterscape attractionsWaterscape = attractionsService.getWaterscapeById(id);
        return Msg.success().add("attractionsWaterscape",attractionsWaterscape);
    }

    @PutMapping("/attractions/updateWaterscape")
    @ResponseBody
    public Msg updateWaterscape(@RequestParam("id") Integer id, AttractionsWaterscape attractionsWaterscape) {
        attractionsWaterscape.setId(id);
        Integer result = attractionsService.updateWaterscape(attractionsWaterscape);
        if (result == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @DeleteMapping("/attractions/deleteWaterscapeById")
    @ResponseBody
    public Msg deleteWaterscapeById(@RequestParam("id")Integer id){
        Integer result = attractionsService.deleteWaterscapeById(id);
        if (result == 1) {
            return Msg.success();
        }
        return Msg.fail();
    }

//    @GetMapping("/user/getUserById/{id}")
//    @ResponseBody
//    public Msg getUserById(@PathVariable("id")Integer id){
//        User user = userService.getUserById(id);
//        return Msg.success().add("user",user);
//    }



    @RequestMapping("/uploadImage")
    @ResponseBody
    public Msg uploadImage(@RequestParam("file") MultipartFile multipartFile){
        System.out.println(multipartFile.getOriginalFilename());
        String fileName = multipartFile.getOriginalFilename();
//        String filePrefixName = fileName.substring(0, fileName.indexOf("."));
//        System.out.println(filePrefixName);
        String newFilePath = filePath;
        File file = new File(newFilePath, fileName);

        if (!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        try{
            multipartFile.transferTo(new File(newFilePath+File.separator+fileName));
            return Msg.success();
        }catch (IOException e){
            e.printStackTrace();
        }
        return Msg.fail();
    }

    @RequestMapping("/uploadHtmlFile")
    @ResponseBody
    public Msg uploadHtmlFile(@RequestParam("htmlFile") MultipartFile multipartFile){
        System.out.println(multipartFile.getOriginalFilename());
        String fileName = multipartFile.getOriginalFilename();
        String newFilePath = "D://ideaProject/country_platform/src/main/resources/static/files/";
        File file = new File(newFilePath, fileName);

        if (!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        try{
            multipartFile.transferTo(new File(newFilePath+File.separator+fileName));
            return Msg.success();
        }catch (IOException e){
            e.printStackTrace();
        }
        return Msg.fail();
    }
}
